package unit22.runtimeAnnotation;

import java.awt.event.ActionListener;
import java.awt.event.InvocationEvent;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ActionListenerInstaller {
	public static void processAnnotation(Object obj){
		try {
			Class<?> cl=obj.getClass();
			for(Method m:cl.getDeclaredMethods()){
				ActionListenerFor a=m.getAnnotation(ActionListenerFor.class);
				if(a!=null){
					Field f=cl.getDeclaredField(a.source());
					f.setAccessible(true);
					addListener(f.get(obj),obj,m);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	private static void addListener(Object source,final  Object param, final Method m) throws Exception {
		// TODO Auto-generated method stub
		InvocationHandler handler=new 	InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				// TODO Auto-generated method stub
				return method.invoke(param);
			}
		};
		Object listener=Proxy.newProxyInstance(null, new Class[] {java.awt.event.ActionListener.class}, handler);
		Method adder=source.getClass().getMethod("addActionListener", ActionListener.class);
		adder.invoke(source, listener);
	}
}
